<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>上传Excel文件并导入数据库</title>
    <link rel="stylesheet" type="text/css" href="/static/layui/css/layui.css" />
{#    <link rel="stylesheet" type="text/css" href="../../static/admin/css/admin.css" />#}
    <style>
        body { font-family: Arial, sans-serif; }
        .container { width: 90%; margin: 0 auto; }
        .form-group { margin-bottom: 15px; }
        .form-group label { display: block; margin-bottom: 5px; }
        .form-group input, .form-group select { width: 100%; padding: 8px; box-sizing: border-box; }
        .btn { padding: 10px 15px; background-color: #007BFF; color: white; border: none; cursor: pointer; align-items: center; justify-content: center; }
        .btn:hover { background-color: #0056b3; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        table, th, td { border: 1px solid #ddd; }
        th, td { padding: 10px; text-align: left; }
        th { background-color: #f2f2f2; }
        .header-container { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <h2>基础表导入</h2>
        <table>
            <tr>
                <th>ECU 特性列表</th>
                <td>
                    <input type="file" id="ecuFeatureFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('ecuFeatureFile', 'EcuFeatureTable')">导入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('EcuFeatureTable')">清空</button>
                </td>
            </tr>
            <tr>
                <th>ECU特性与安全属性映射表</th>
                <td>
                    <input type="file" id="featureMapSecurityFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('featureMapSecurityFile', 'FeatureMapSecurityAttrTable')">导入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('FeatureMapSecurityAttrTable')">清空</button>
                </td>
            </tr>
            <tr>
                <th>威胁评估参数表</th>
                <td>
                    <input type="file" id="threatAssessmentFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('threatAssessmentFile', 'ThreadAssessmentParaTable')">导入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('ThreadAssessmentParaTable')">清空</button>
                </td>
            </tr>
            <tr>
                <th>风险评估参数表</th>
                <td>
                    <input type="file" id="riskAssessmentFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('riskAssessmentFile', 'RiskAssessmentParaTable')">导入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('RiskAssessmentParaTable')">清空</button>
                </td>
            </tr>
            <tr>
                <th>ITEM影响等级参数表</th>
                <td>
                    <input type="file" id="itemImpactLevelFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('itemImpactLevelFile', 'ItemImpactLevelTable')">导入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('ItemImpactLevelTable')">清空</button>
                </td>
            </tr>
            <tr>
                <th>TARA评估规则表</th>
                <td>
                    <input type="file" id="taraRuleMappingFile" accept=".xlsx, .xls">
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" onclick="uploadFile('taraRuleMappingFile', 'TaraRuleMappingTable')">导 入</button>
                    <button type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-border" onclick="clearTable('TaraRuleMappingTable')">清空</button>
                </td>
            </tr>
        </table>
        <h2>用户数据批量导入</h2>
        <table>
            <tr>
                <th>7.ECU清单</th>
                <td>
                    <input type="file" id="ecuFile" accept=".xlsx, .xls">
                    <button type="button" class="btn" onclick="uploadFile('ecuFile', 'EcuTable')">
                        <i class="layui-icon" style="font-size: 16px; color: whitesmoke;">&#xe62f;</i> 清空导入</button>
                </td>
            </tr>
            <tr>
                <th>8.功能相关性数据</th>
                <td>
                    <input type="file" id="funcRelationFile" accept=".xlsx, .xls">
                    <button type="button" class="btn" onclick="uploadFile('funcRelationFile', 'FuncRelationTable')">
                        <i class="layui-icon" style="font-size: 16px; color: whitesmoke;">&#xe62f;</i> 清空导入</button>
                </td>
            </tr>
            <tr>
                <th>9.整车信息安全需求库</th>
                <td>
                    <input type="file" id="cybersecurityReqFile" accept=".xlsx, .xls">
                    <button type="button" class="btn" onclick="uploadFile('cybersecurityReqFile', 'CybersecurityReqTable')">导入</button>
                </td>
            </tr>
            <tr>
                <th>10.TARA数据表</th>
                <td>
                    <input type="file" id="taraTableFile" accept=".xlsx, .xls">
                    <button type="button" class="btn" onclick="uploadFile('taraTableFile', 'taraTable')">导入</button>
                </td>
            </tr>
        </table>
    </div>

    <script src="../../static/admin/layui2.9.13/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/admin/js/common.js" type="text/javascript" charset="utf-8"></script>
    <script>
        function uploadFile(fileInputId, tableName) {
            var fileInput = document.getElementById(fileInputId);
            console.log('fileInput:', fileInput)
            var file = fileInput.files[0];
            if (!file) {
                alert('请选择一个文件');
                return;
            }

            var formData = new FormData();
            console.log('file:', file)
            console.log('table:', tableName)

            formData.append('file', file);
            formData.append('tableName', tableName);

            fetch('/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                alert(data.msg);
            })
            .catch(error => {
                console.error('Error:', error);
                alert('上传失败');
            });
        }

        function clearTable(tableName) {
            fetch(`/clearTable/${tableName}`, {
                method: 'DELETE'
            })
            .then(response => response.json())
            .then(data => {
                alert(data.msg);
            })
            .catch(error => {
                console.error('Error:', error);
                alert('清空失败');
            });
        }
    </script>
</body>
</html>